package com.lineage.server.timecontroller.event;

import com.lineage.server.datatables.UBTable;
import com.lineage.server.model.L1UltimateBattle;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.World;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class UbTime extends TimerTask {
  private static final Log _log = LogFactory.getLog(UbTime.class);
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 16500;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 16500L, 16500L);
  }
  
  public void run() {
    try {
      checkUbTime();
    } catch (Exception e) {
      _log.error("无线大赛时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      UbTime timer = new UbTime();
      timer.start();
    } 
  }
  
  private static void checkUbTime() {
    Iterator<L1UltimateBattle> iterator = UBTable.getInstance().getAllUb().iterator();
    while (iterator.hasNext()) {
      L1UltimateBattle ub = iterator.next();
      if (ub.checkUbTime() && !ub.isActive()) {
        ub.start();
        World.get().broadcastPacketToAll((ServerBasePacket)new S_SystemMessage("【" + ub.getName() + "】５分钟后比赛将开始，想参赛者请赶快入场。"));
      } 
    } 
  }
}
